package org.example.panel.studentpanel;
import javax.swing.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class DeleteStudentPanel extends JPanel {
    public DeleteStudentPanel(){
        //组件准备
        JTextField num = new JTextField(15);
        JButton deleteStudent = new JButton("删除学生");
        Box numBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        deleteStudent.addActionListener((ActionEvent e) -> {
            if(num.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null,
                        "学号不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(num.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "学号不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(StudentOperation.isNotStudent(num.getText())){
                JOptionPane.showMessageDialog(null,
                        "学生不存在", null, JOptionPane.WARNING_MESSAGE);
            }else{
                StudentOperation.deleteStudent(num.getText());
                JOptionPane.showMessageDialog(null,
                        "删除学生成功", null, JOptionPane.WARNING_MESSAGE);
            }
        });
        //添加组件
        numBox.add(new JLabel("学号： "));
        numBox.add(num);
        box.add(numBox);
        box.add(Box.createVerticalStrut(5));
        box.add(deleteStudent);
        add(box);
    }
}
